<?php
/**
 * Magento Enterprise Edition
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Magento Enterprise Edition End User License Agreement
 * that is bundled with this package in the file LICENSE_EE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.magento.com/license/enterprise-edition
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magento.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magento.com for more information.
 *
 * @category    Mage
 * @package     Mage_Adminhtml
 * @copyright Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
 * @license http://www.magento.com/license/enterprise-edition
 */

/**
 * Customer edit block
 *
 * @category   Mage
 * @package    Mage_Adminhtml
 * @author      Magento Core Team <core@magentocommerce.com>
 */
class Silk_Qslb_Block_Adminhtml_Catalog_Product_Edit extends Mage_Adminhtml_Block_Widget
{
    public function __construct()
    {
        parent::__construct();
        $this->setTemplate('catalog/product/edit.phtml');
        $this->setId('product_edit');
    }

    /**
     * Retrieve currently edited product object
     *
     * @return Mage_Catalog_Model_Product
     */
    public function getProduct()
    {
        return Mage::registry('current_product');
    }

    protected function _prepareLayout()
    {
        if (!$this->getRequest()->getParam('popup')) {
            $this->setChild('back_button',
                $this->getLayout()->createBlock('adminhtml/widget_button')
                    ->setData(array(
                        'label'     => Mage::helper('catalog')->__('Back'),
                        'onclick'   => 'setLocation(\''
                            . $this->getUrl('*/*/', array('store'=>$this->getRequest()->getParam('store', 0))).'\')',
                        'class' => 'back'
                    ))
            );
        } else {
            $this->setChild('back_button',
                $this->getLayout()->createBlock('adminhtml/widget_button')
                    ->setData(array(
                        'label'     => Mage::helper('catalog')->__('Close Window'),
                        'onclick'   => 'window.close()',
                        'class' => 'cancel'
                    ))
            );
        }

        $session = Mage::getSingleton('admin/session');
        $roleId = $session->getUser()->getRoleId();
        $storeId = $this->getRequest()->getParam('store');
        $canDelete = true;
        $canSave = true;
        if($roleId == Mage::helper('zgb')->getRoleId('warehouse') || $roleId == Mage::helper('zgb')->getRoleId('dealer'))
        {
            $canDelete = false;
            if(!$storeId)
            {
                $canSave = false;
            }
        }




        if (!$this->getProduct()->isReadonly()) {
            // $this->setChild('reset_button',
            //     $this->getLayout()->createBlock('adminhtml/widget_button')
            //         ->setData(array(
            //             'label'     => Mage::helper('catalog')->__('Reset'),
            //             'onclick'   => 'setLocation(\''.$this->getUrl('*/*/*', array('_current'=>true)).'\')'
            //         ))
            // );
            if($canSave)
            {
                $this->setChild('save_button',
                    $this->getLayout()->createBlock('adminhtml/widget_button')
                        ->setData(array(
                            'label'     => Mage::helper('catalog')->__('Save'),
                            'onclick'   => 'productForm.submit()',
                            'class' => 'save'
                        ))
                );
            }
        }

        if (!$this->getRequest()->getParam('popup')) {
            if (!$this->getProduct()->isReadonly()) {
                if($canSave)
                {
                    $this->setChild('save_and_edit_button',
                        $this->getLayout()->createBlock('adminhtml/widget_button')
                            ->setData(array(
                                'label'     => Mage::helper('catalog')->__('Save and Continue Edit'),
                                'onclick'   => 'saveAndContinueEdit(\''.$this->getSaveAndContinueUrl().'\')',
                                'class' => 'save'
                            ))
                    );
                }
            }

            /*  if ($this->getProduct()->isDeleteable() && $canDelete) {

                  $confirmationMessage = Mage::helper('core')->jsQuoteEscape(
                      Mage::helper('catalog')->__('Are you sure?')
                  );
                  $this->setChild('delete_button',
                      $this->getLayout()->createBlock('adminhtml/widget_button')
                          ->setData(array(
                              'label'     => Mage::helper('catalog')->__('删除商品'),
                              'onclick'   => 'confirmSetLocation(\'' . $confirmationMessage
                                  . '\', \'' . $this->getDeleteUrl() . '\')',
                              'class'  => 'delete'
                          ))
                  );
              }*/

            // if ($this->getProduct()->isDuplicable()) {
            //     $this->setChild('duplicate_button',
            //     $this->getLayout()->createBlock('adminhtml/widget_button')
            //         ->setData(array(
            //             'label'     => Mage::helper('catalog')->__('Duplicate'),
            //             'onclick'   => 'setLocation(\'' . $this->getDuplicateUrl() . '\')',
            //             'class'  => 'add'
            //         ))
            //     );
            // }
        }

        return parent::_prepareLayout();
    }

    public function getBackButtonHtml()
    {
        return $this->getChildHtml('back_button');
    }

    public function getCancelButtonHtml()
    {
        return $this->getChildHtml('reset_button');
    }

    public function getSaveButtonHtml()
    {
        return $this->getChildHtml('save_button');
    }

    public function getSaveAndEditButtonHtml()
    {
        return $this->getChildHtml('save_and_edit_button');
    }

    public function getDeleteButtonHtml()
    {
        return $this->getChildHtml('delete_button');
    }

    public function getDuplicateButtonHtml()
    {
        return $this->getChildHtml('duplicate_button');
    }

    public function getValidationUrl()
    {
        return $this->getUrl('*/*/validate', array('_current'=>true));
    }

    public function getSaveUrl()
    {
        return $this->getUrl('*/*/save', array('_current'=>true, 'back'=>null));
    }

    public function getSaveAndContinueUrl()
    {
        return $this->getUrl('*/*/save', array(
            '_current'   => true,
            'back'       => 'edit',
            'tab'        => '{{tab_id}}',
            'active_tab' => null
        ));
    }

    public function getProductId()
    {
        return $this->getProduct()->getId();
    }

    public function getProductSetId()
    {
        $setId = false;
        if (!($setId = $this->getProduct()->getAttributeSetId()) && $this->getRequest()) {
            $setId = $this->getRequest()->getParam('set', null);
        }
        return $setId;
    }

    public function getIsGrouped()
    {
        return $this->getProduct()->isGrouped();
    }

    public function getDeleteUrl()
    {
        return $this->getUrl('*/*/delete', array('_current'=>true));
    }

    public function getDuplicateUrl()
    {
        return $this->getUrl('*/*/duplicate', array('_current'=>true));
    }

    public function getHeader()
    {
        $header = '';
        if ($this->getProduct()->getId()) {
            $header = $this->escapeHtml($this->getProduct()->getName());
        }
        else {
            $header = Mage::helper('catalog')->__('New Product');
        }
        if ($setName = $this->getAttributeSetName()) {
            $header.= ' (' . $setName . ')';
        }
        return $header;
    }

    public function getAttributeSetName()
    {
        if ($setId = $this->getProduct()->getAttributeSetId()) {
            $set = Mage::getModel('eav/entity_attribute_set')
                ->load($setId);
            return $set->getAttributeSetName();
        }
        return '';
    }

    public function getIsConfigured()
    {
        if ($this->getProduct()->isConfigurable()
            && !($superAttributes = $this->getProduct()
                ->getTypeInstance(true)
                ->getUsedProductAttributeIds($this->getProduct()))
        ) {
            $superAttributes = false;
        }

        return !$this->getProduct()->isConfigurable() || $superAttributes !== false;
    }

    public function getSelectedTabId()
    {
        return addslashes(htmlspecialchars($this->getRequest()->getParam('tab')));
    }
}